package num06;

import java.util.LinkedList;

/**
 * @author : DuJiabao
 * @Date : 2021/1/24 0:38
 * @Project : sword-finger-offer
 * @File : null.java
 * @Desc : 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 * https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
 * 用栈的原理 LinkedList继承了栈了。可直接用push、pop。直观一点。
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        LinkedList<Integer> stack = new LinkedList<>();
        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }
        int[] res = new int[stack.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = stack.pop();
        }
        return res;
    }
}
